package com.lst.servlet;

import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.util.StringUtils;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.lst.base.entity.BaseRequest;
import com.lst.base.entity.BaseResponse;
import com.lst.common.CommCode;
import com.lst.entity.request.CliNumReqPara;
import com.lst.entity.response.CliNumResPara;
import com.lst.model.ClientNum;
import com.lst.service.ClientNumService;
import com.lst.utils.RequestUtils;

/**
 * @ClassName: ClientNumListServlet
 * @Description: 我的收藏
 * @author zmm
 * @date 2017年10月8日 上午10:23:57
 *
 */
public class ClientNumListServlet extends BaseServlet {

	private static final long serialVersionUID = 1L;

	private ClientNumService clientNumService = ctx.getBean(ClientNumService.class); 

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response) {
		doPost(request, response);
	}

	@Override
	public void doPost(HttpServletRequest request, HttpServletResponse response) {

		CliNumResPara resPara = new CliNumResPara();

		try {
			CliNumReqPara reqPara = getReqPara(request);

			if(isCheckField(reqPara,resPara)){
				Integer clientid = Integer.valueOf(reqPara.getUserid());
				Integer numtype = Integer.valueOf(reqPara.getNumtype());
				Integer page = Integer.valueOf(reqPara.getPage());
				Integer pagesize = Integer.valueOf(reqPara.getPagesize());

				Map<String, Object> reqMap = new HashMap<String, Object>();

				reqMap.put("enabled", true);
				reqMap.put("isDeleted", false);
				reqMap.put("clientid", clientid);
				reqMap.put("orderByClause", "cm.updatedate desc");//收藏时间
				reqMap.put("numtype", numtype);

				PageBounds pageBounds = new PageBounds(page , pagesize);	

				List<ClientNum> cf = clientNumService.queryList(reqMap, pageBounds);
				if(cf!=null&&cf.size()>0){
					for(ClientNum clientNum:cf){
						if(clientNum.getActtype()==52&&clientNum.getDyDynamicUrl()!=null){
							String pictureUrl=clientNum.getDyDynamicUrl();
							if(pictureUrl!=null){
								String pictureUrl2=pictureUrl.split(",")[0];
								clientNum.setDyDynamicUrl(pictureUrl2);
							}
						}
						if(clientNum.getActtype()==53&&clientNum.getQuPictureUrl()!=null){
							String pictureUrl=clientNum.getQuPictureUrl();
							if(pictureUrl!=null){
								String pictureUrl2=pictureUrl.split(",")[0];
								clientNum.setQuPictureUrl(pictureUrl2);
							}
						}
					}
				}
			
				resPara.setCnfavorite(cf);
				resPara.setCode(CommCode.M_Y000000);
				resPara.setMessage(CommCode.M_Y000001);				

			}

		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0001);

			logger.error("doPost error: ", e);
		}

		try {
			response.setContentType("application/json;charset=utf-8");
			response.setHeader("pragma", "no-cache");
			response.setHeader("cache-control", "no-cache");

			Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").excludeFieldsWithoutExposeAnnotation().serializeNulls().create();
			String jsonResult = gson.toJson(resPara);

		//	logger.info("ClientFavoriteListServlet jsonResult: " + jsonResult);

			PrintWriter out = response.getWriter();
			out.print(jsonResult);
			out.flush();
			out.close();

		//	logger.info("doPost end run(s): " + DateUtil.calLastedTime(startDate));
		} catch (Exception e) {
			resPara.setCode(CommCode.M_A000000);
			resPara.setMessage(CommCode.M_SYS0003);

			logger.error("doPost error: ", e);
		}
	}

	@Override
	public CliNumReqPara getReqPara(HttpServletRequest request) {

		CliNumReqPara reqPara = null;

		try {
			BaseRequest baseRequest = RequestUtils.getRequestPara(request, new CliNumReqPara());

			reqPara = (CliNumReqPara) baseRequest.clone();

			String userid = request.getParameter("userid");
			String numtype = request.getParameter("numtype");
			String page = request.getParameter("page");
			String pagesize = request.getParameter("pagesize");

//			logger.info("reqPara userid: " + userid);
//			logger.info("reqPara numtype: " + numtype);	
//			logger.info("reqPara page: " + page);
//			logger.info("reqPara pagesize: " +pagesize);

			reqPara.setUserid(userid);
			reqPara.setNumtype(numtype);
			reqPara.setPage(page);
			reqPara.setPagesize(pagesize);

		} catch (Exception e) {
			logger.error("getReqPara error: ", e);
		}

		return reqPara;
	}

	@Override
	public boolean isCheckField(BaseRequest reqPara, BaseResponse resPara) {
		boolean flag = false;

		CliNumReqPara cfReqPara = (CliNumReqPara) reqPara;
		CliNumResPara cfResPara = (CliNumResPara) resPara;

		if (StringUtils.isEmpty(cfReqPara.getUserid())) {
			cfResPara.setCode(CommCode.M_A000000);
			cfResPara.setMessage(CommCode.M_P000002);
		}else if (StringUtils.isEmpty(cfReqPara.getNumtype())) {
			cfResPara.setCode(CommCode.M_A000000);
			cfResPara.setMessage("变化类型为空");
		} else {
			flag = true;
		}

		return  flag;
	}
}
